package cn.fancy.sdk.pay.view;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import cn.fancy.sdk.common.utils.DimensionUtil;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.entity.DevBase.DevEnv;
import cn.fancy.sdk.pay.ChargeAbstractView;
import cn.fancy.sdk.pay.GuildPayActivity;
import cn.fancy.sdk.pay.ali.AlipayView;
import cn.fancy.sdk.pay.entity.ChargeListsResultEntity;
import cn.fancy.sdk.pay.entity.PaymentId;
import cn.fancy.sdk.pay.utils.PayUtil;
import cn.fancy.sdk.pay.weixin.WeixinPayView;

public class PayListView extends ChargeAbstractView {
    
    private final GuildPayActivity activity;
    
    private ListView listView;
    
    private PaymentListAdapter adapter;
    
    private int selectPosition = -1;
    
    private final int screenHeigh;
    
    /*
     * 支付列表点击事件
     */
    private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {
        
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
            ChargeListsResultEntity chargeIntem = (ChargeListsResultEntity) parent
                .getAdapter().getItem(position);
            
            setRight(chargeIntem, position);
        }
    };
    
    public PayListView(GuildPayActivity activity) {
        this.activity = activity;
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenHeigh = dm.heightPixels;
    }
    
    @Override
    public View createView() {
        View view = LayoutInflater.from(activity).inflate(
            ResUtil.getLayoutId(activity, "guild_pay_paylist_view"), null);
        listView = (ListView) view.findViewById(ResUtil.getId(activity, "guild_paylist"));
        TextView blank = (TextView) view.findViewById(ResUtil.getId(activity, "guild_paylist_blank"));
        blank.setHeight(screenHeigh);
        activityCreated();
        return view;
    }
    
    @Override
    public void activityCreated() {
        adapter = new PaymentListAdapter(activity.chargeLists);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(mOnItemClickListener);
        showLastCharge();
    }
    
    /*
     * 左侧显示框
     */
    private void setRight(ChargeListsResultEntity chargeIntem, int position) {
        int paymentId = chargeIntem.getPaymentId();
        PayUtil.putLastPaymentId(activity, paymentId);
    
        switch (paymentId) {
            // 支付宝和微信扫码，都是走AlipayH5Activity的逻辑
            case PaymentId.ALIPAY_ID:
                // 支付宝扫码
            case PaymentId.ALIPAY_SCAN_CODE:
                // 微信扫码
            case PaymentId.WEIXIN_SCAN_CODE:
                AlipayView alipayView = new AlipayView(activity, paymentId);
                activity.replaceRight(alipayView);
                break;
            case PaymentId.WEIXIN_ID:
                // 微信
                WeixinPayView wxView = new WeixinPayView(activity, paymentId);
                activity.replaceRight(wxView);
                break;
        }
        selectPosition = position;
        adapter.notifyDataSetInvalidated();
    }
    
    /*
     * 显示上一次的支付方式，如果没有。就显示支付帮助
     */
    private void showLastCharge() {
        ChargeListsResultEntity lastChargeList = null;
        int lastPayId = PayUtil.getLastPaymentID(activity);
        int lastPostion = -1;
        
        for (int i = 0; i < adapter.getCount(); i++) {
            
            ChargeListsResultEntity chargeList = (ChargeListsResultEntity) adapter
                .getItem(i);
            if (lastPayId == chargeList.getPaymentId()) {
                lastChargeList = chargeList;
                lastPostion = i;
                break;
            }
        }
        
        if (lastChargeList != null) {
            setRight(lastChargeList, lastPostion);
        }
    }
    
    
    class PaymentListAdapter extends BaseAdapter {
        
        private final ChargeListsResultEntity[] chargeLists;
        
        PaymentListAdapter(ChargeListsResultEntity[] chargeLists) {
            this.chargeLists = chargeLists;
        }
        
        @Override
        public int getCount() {
            return chargeLists.length;
        }
        
        @Override
        public Object getItem(int position) {
            return chargeLists[position];
        }
        
        @Override
        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            
            LinearLayout payListLayout = (LinearLayout) convertView;
            if (payListLayout == null) {
                LayoutInflater inflater = LayoutInflater.from(activity);
                payListLayout = (LinearLayout) inflater.inflate(ResUtil.getLayoutId(activity, "guild_pay_paylist_adaper"), null);
            }
            
            LinearLayout linearLayout = (LinearLayout) payListLayout.findViewById(ResUtil.getId(activity, "guild_pay_paylist_ll"));
            LayoutParams linearParams = (LayoutParams) linearLayout.getLayoutParams();
            
            int height = linearParams.height;
            int topHeiht = DimensionUtil.dip2px(activity, 82);
            
            if (DevEnv.LOGO == 0) {
                topHeiht = DimensionUtil.dip2px(activity, 62);
            }
            
            if ((height * chargeLists.length) > (screenHeigh - topHeiht)) {// 有滚动
                int lineHeight = DimensionUtil.dip2px(activity, 8);
                linearParams.height = (screenHeigh - topHeiht - lineHeight * chargeLists.length) / chargeLists.length;
            }
            linearLayout.setLayoutParams(linearParams);
            
            ImageView iv = (ImageView) payListLayout.findViewById(ResUtil.getId(activity, "guild_pay_paylist_iv"));
            TextView tv = (TextView) payListLayout.findViewById(ResUtil.getId(activity, "guild_pay_paylist_tv"));
            
            final ChargeListsResultEntity charge = chargeLists[position];
            
            if (selectPosition == position) {
                tv.setTextColor(0xff434343);
                if (position == 0) {
                    payListLayout.setBackgroundDrawable(activity.getResources().getDrawable(ResUtil.getDrawableId(activity, "guild_paylist_one_sel_bg")));
                } else {
                    payListLayout.setBackgroundDrawable(activity.getResources().getDrawable(ResUtil.getDrawableId(activity, "guild_paylist_sel_bg")));
                }
            } else {
                payListLayout.setBackgroundDrawable(activity.getResources().getDrawable(ResUtil.getDrawableId(activity, "guild_paylist_bg")));
                tv.setTextColor(0xff8b8686);
            }
            
            switch (charge.getPaymentId()) {
                case PaymentId.ALIPAY_ID:
                case PaymentId.ALIPAY_SCAN_CODE:
                    // 支付宝
                    iv.setBackgroundResource(ResUtil.getDrawableId(activity, "guild_zhifubao"));
                    break;
                case PaymentId.WEIXIN_ID:
                case PaymentId.WEIXIN_SCAN_CODE:
                    // weixin
                    iv.setBackgroundResource(ResUtil.getDrawableId(activity, "guild_weixin_icon"));
                    break;
            }
            tv.setText(charge.getPaymentName());
            return payListLayout;
        }
    }
}